package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.constants.SystemConstants;
import com.blog.domain.ResponseResult;
import com.blog.domain.entity.Comment;
import com.blog.domain.vo.CommentVo;
import com.blog.domain.vo.PageVo;
import com.blog.enums.AppHttpCodeEnum;
import com.blog.exception.SystemException;
import com.blog.mapper.CommentMapper;
import com.blog.service.CommentService;
import com.blog.service.UserService;
import com.blog.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-09-08 16:35:02
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

	@Autowired
	private UserService userService;

	/**
	 * 查看评论
	 *
	 * @param commentType
	 * @param articleId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
     //查询对应文章的根评论
     //对articleId进行判断
     LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();

	 lambdaQueryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
     //根评论rootid为-1
     lambdaQueryWrapper.eq(Comment::getRootId,-1);
	 //评论类型
		lambdaQueryWrapper.eq(Comment::getType,commentType);
     //分页查询
     Page<Comment> page = new Page(pageNum,pageSize);
     page(page,lambdaQueryWrapper);
     long total = page.getTotal();
     List<Comment> records = page.getRecords();

		List<CommentVo> commentVos = toCommentVoList(records);
		//查询所有根评论对应的子评论集合，并赋值给对应的属性
		for (CommentVo commentvo : commentVos) {
			//查询对应的子评论,根据根评论id
			List<CommentVo> children = getChildren(commentvo.getId());

			//赋值
			commentvo.setChildren(children);
		}



		return ResponseResult.okResult(new PageVo(commentVos,total));
	}

	/**评论
	 *
	 * @param comment
	 * @return
	 */
	@Override
	public ResponseResult addComment(Comment comment) {
		//评论内容不能为空
		if (!StringUtils.hasText(comment.getContent())){
			throw new SystemException(AppHttpCodeEnum.CONTENT_NO_NULL);
		}
		save(comment);
		return ResponseResult.okResult();
	}

	/**根据根评论id查询所对应的子评论的集合
	 *
	 * @param id
	 * @return
	 */
	private List<CommentVo> getChildren(Long id){
		LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Comment::getRootId,id);
		queryWrapper.orderByAsc(Comment::getCreateTime);
		List<Comment> comments = list(queryWrapper);

		List<CommentVo> commentVos = toCommentVoList(comments);

		return commentVos;
	}

	/**
	 * 将comment转换为commentvo 并填充一些字段
	 * @param list
	 * @return
	 */
	private List<CommentVo> toCommentVoList(List<Comment> list){
		List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
		//遍历VO集合
		//通过createBy查询用户的昵称和头像并赋值
		for (CommentVo commentVo : commentVos) {
			String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
			commentVo.setUsername(nickName);
			//通过toCommentUserId 查询用户的昵称并赋值
			//如果tocommnetuserid 不为-1 ，进行查询,不为-1就是回复了评论
			if (commentVo.getToCommentUserId() != -1){
				String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
				commentVo.setToCommentUserName(toCommentUserName);
				String avatar = userService.getById(commentVo.getCreateBy()).getAvatar();
				commentVo.setAvatar(avatar);
			}
			if (commentVo.getToCommentUserId() == -1){
				String avatar = userService.getById(commentVo.getCreateBy()).getAvatar();
				commentVo.setAvatar(avatar);
			}
		}
			return commentVos;
	}
}
